const {Area} = require('../db/model/index')

const createArea = async ({name, parentId, level}) => {
  const result = await Area.create({
    name,
    parentId,
    level
  })

  const data = result.dataValues

  return data
}

const updateArea = async ({name, id}) => {
  const whereOpt = {
    id
  }

  const result = await Area.update({
    name
  }, {
    where: whereOpt
  })

  return result[0] > 0
}

const delArea = async (id) => {

  const result = await Area.destroy({
    where: {
      id
    }
  })

  return result > 0
}

const getAllArea = async () => {
  const result = await Area.findAll()

  const data = result.map(item => item.dataValues)

  return data
}

module.exports = {
  createArea,
  updateArea,
  delArea,
  getAllArea
}
